// 包装 i-select 选择器
import { extractModel } from '@/utils/tools'

export default {
    name: 'c-i-select',
    functional:true,

    render (h, context){

        let {value, callback, conf} = extractModel(context);
        let {
            
            size,
            options,

            multiple,
            disabled,
            clearable,
            filterable,
            remote,
            loading,
            loadingRext,
            label,
            placeholder,
            placement,
            transfer,
            
            
            labelInValue,
            notFoundText,
            remoteMethod,

            onChange,
            
        } = conf


        function buildOptions(optons){
            let cache = []
            options = options || []
            
            options.forEach(item => {
                
               cache.push(
                   <i-option value={item.value} key={item.value}>
                       {item.label}
                   </i-option>
               )
                
            });

            return cache
        }
       
        function change(arg){

            if(onChange){
                onChange(arg)
            }

            callback(arg)
        }
        
        return (

            <i-select

            multiple={multiple}
            disabled={disabled}
            clearable={clearable}
            filterable={filterable}
            remote={remote}
            loading={loading}
            loadingRext={loadingRext}
            label={label}
            placeholder={placeholder}
            placement={placement}
            transfer={transfer}
            
            
            label-in-value={labelInValue}
            not-found-text={notFoundText}
            remote-method={remoteMethod}
            
            value={value}
            onChange={change}
            onInput={change}
            size={size || 'small'}>

                {buildOptions(options)}

            </i-select>
            
        )

    }
}